package edu.lingnan.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.lingnan.entity.Answer;
import edu.lingnan.entity.Feedback;
import edu.lingnan.mapper.FeedbackMapper;
import edu.lingnan.service.AnswerService;
import edu.lingnan.mapper.AnswerMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author 26439
 * @description 针对表【tb_answer】的数据库操作Service实现
 * @createDate 2022-10-02 19:42:04
 */
@Service
public class AnswerServiceImpl extends ServiceImpl<AnswerMapper, Answer>
        implements AnswerService {

    @Resource
    private FeedbackMapper feedbackMapper;

    @Resource
    private ThreadPoolExecutor executor;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void answer(Answer answer) {
        CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
            //1.先将反馈表的反馈状态至为已处理
            Feedback feedback = new Feedback();
            feedback.setId(answer.getFeedbackId())
                    .setStatus(1);
            feedbackMapper.updateById(feedback);
        }, executor);

        CompletableFuture<Void> future1 = CompletableFuture.runAsync(() -> {
            //2.将反馈信息插入
            answer.setAnswerTime(LocalDateTime.now());
            this.save(answer);
        }, executor);

        CompletableFuture.allOf(future1, future);
    }
}




